// pages/order/order.js

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 10,
    list: [],
    date: new Date().getTime(),
    id: null,
    show: false,
    cdate: '',
    showDate: false,
    price: 0,
    number: 0,
    menu_price: 0,
    activeNames: ['0']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },
  getList() {
    let list = []
    app.requestUrl('/menu/order', {page: this.data.page, limit: this.data.limit}, res=> {
      if(res.data.length ==0) {
        wx.showToast({
          title: '没有了',
          icon: 'none',
          duration: 2000
        })
        return 
      }
      if(this.data.page == 1) {
        list = res.data
       }else{
         list = this.data.list
         for(let i in res.data) {
           list.push(res.data[i])
         }
       }
       this.setData({
         list
       })
    })
  },
  onChange(e){
    this.setData({
      activeNames: e.detail
    })
  },
  moreList(){
    this.setData({
      page: this.data.page + 1
    })
    this.getList()
  },
  changeNumber(e) {
    let id = e.currentTarget.dataset.id
    let number = e.currentTarget.dataset.num
    let cdate = e.currentTarget.dataset.date
    let price = e.currentTarget.dataset.price
    let menu_price = e.currentTarget.dataset.menuprice
    let show = true
    this.setData({
      show, id, number ,cdate, price, menu_price
    })
  },
  onShowdate() {
    this.setData({
      showDate: true
    })
  },
  onClose() {
    this.setData({
      showDate: false
    })
  },
  close() {
    this.setData({
      show: false
    })
  },
  plus(e) {
    let number = this.data.number
    if(e.currentTarget.dataset.id == 1) {
      number = number + 1
    }else{
      number = number - 1
      if(number == 0)number = 1
    }
    let price = this.data.menu_price * number * 100
    this.setData({ number, price })
  },
  onSubmit(e) {
    app.requestUrl('/menu/postEdit', {id: this.data.id, number: this.data.number, date: this.data.cdate}, ret=> {
      let that = this
      wx.showToast({
        title: '修改成功！',
        icon: 'none',
        duration: 2000,
        success: res=> {
          setTimeout(function() {
            that.setData({
              show: false
            })
            that.getList()
          }, 2000)
        }
      }) 
      
    }, 'POST', false)
  },
  deleteOrder(e) {
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '您确定要删除订单吗？',
      success : (res)=> {
        if (res.confirm) {
          app.requestUrl('/menu/deleteOrder', {id}, res=> {
            wx.showToast({
              title: '删除成功！',
              icon: 'none',
              duration: 2000
            }) 
            this.getList()
            this.setData({
              activeNames: []
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  confirmDate(e){
    console.log(e)
    let date = new Date(e.detail)
    this.setData({
      cdate: date.toLocaleDateString(),
      showDate: false
    })
    
  },
  cancelDate() {
    this.setData({
      showDate: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})